package com.cykj.pos.service.Impl;

import com.cykj.pos.domain.*;
import com.cykj.pos.mapper.IncomingPartsMapper;
import com.cykj.pos.service.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.cykj.common.core.domain.AjaxResult;

import java.text.SimpleDateFormat;

/**
 * @author SunJinDuo
 * @date 2022/7/15
 */
@Service
public class IncomingPartsServiceImpl implements IncomingPartsService {

    private static final Logger logger = LoggerFactory.getLogger(IncomingPartsServiceImpl.class);

    @Autowired
    private IncomingPartsMapper incomingPartsMapper;

    @Autowired
    private Rule_oneService ruleOneService;

    /**
     * @author: haoran
     * @date: 2022/7/15
     * 方法名称： 入件接口定时任务主方法
     */
    @Override
    @Transactional
    public void processingData() throws Exception {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String startTime = df.format(System.currentTimeMillis());
        logger.info("<<<<<--- 实现方法开始时间：" + startTime + " --->>>>> -");
        // 查询队列信息，只查询未处理的数据
        logger.info("<<<<<--- 查询队列信息，只查询未处理的数据 --->>>>> -");
        IncomingPartsEntity incomingPartsEntity = incomingPartsMapper.processingData();
        logger.info("<<<<<--- 查询完的队列信息为：" + incomingPartsEntity + " --->>>>> -");
        // 调用规则调用方法
        logger.info("<<<<<--- 调用规则调用方法 --->>>>> -");
        this.ruleInvocation(incomingPartsEntity);
    }

    /**
     * @author: haoran
     * @date: 2022/7/15
     * 方法名称： 规则调用方法
     */
    @Transactional
    public String ruleInvocation(IncomingPartsEntity incomingPartsEntity) throws Exception {
        logger.info("<<<<<--- 规则调用方法，传入的参数：" + incomingPartsEntity + " --->>>>> -");
        AjaxResult ajaxResult = new AjaxResult();
        // 将数据状态改成处理中
        incomingPartsMapper.updateType(incomingPartsEntity.getId());
        // TODO 假数据
        incomingPartsEntity.setChannel("a");
        String[] a = incomingPartsEntity.getChannel().split("，");
        logger.info("<<<<<--- 规则调用方法，数组a的数据为：" + a + " --->>>>> -");
        for (String str : a) {
            if (("a").equals(str)) {
                ajaxResult = ruleOneService.newStartup(incomingPartsEntity.getId());
            } else if (("b").equals(str)) {
                // 调用规则2
            } // 如果有新规则，就继续加else if
        }
        // TODO return的东西约定完了之后再说
        return null;
    }

}
